<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

# INFORMATION:
# dashboard_client_window.php 
# 	- this script is responsible for the handling of data for the client profile window
# 	
#	the script has two (2) modes:
#		- display - gets the data from the database using DBHandle.php class
#		- profile update processing - provides error checking and validation before submitting information to the database using DBHandle.php class
#

define('ACCESS', true);	

session_start();	
	
# set error checking levels #		
	# enable these lines when site is to be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');

# include some needed files #
	$absRoot = '../../';
	include($absRoot . 'lib/PEAR/PEAR/Sigma.php');	
	include($absRoot . 'src/ClientHandle.php');

# instantiate predifined classes #
	$template = new HTML_Template_Sigma($absRoot . 'templates', $absRoot . 'templates-cache');															

# page variables #	
	$page_title = 'De La Salle University | Donations Management System ';
	$css = 'css/client.css';
											
# load template to buffer #
	$template->loadTemplateFile('windows/clientProfile.html');	

$id = $_GET["client_id"];
$client = new ClientHandle();
$ProfileArray=$client->getClientProfile($id);

if($ProfileArray)
{
	
	//Type
	$ClientType="";
	$clienttype=$ProfileArray["client_type"];
	if($clienttype==0)
	$ClientType="";
	else if ($clienttype==1)
	$ClientType="Referred";
	//Name
	$LastName=$ProfileArray["client_lastname"];
	$FirstName=$ProfileArray["client_firstname"];
	$MiddleName=$ProfileArray["client_middlename"];
	$ClientName=$LastName . ", " . $FirstName . " " . $MiddleName;
	//Gender
	$ClientGender=$ProfileArray["client_gender"];
	//Birthday
	$ClientBirthday=$ProfileArray["client_birthday"];
	$BirthMonth=substr($ClientBirthday,5,2);
	$BirthDay=substr($ClientBirthday,8,2);
	$BirthYear=substr($ClientBirthday,0,4);
	switch ($BirthMonth)
	{
		case 1: $BirthMonth="January"; break;
		case 2: $BirthMonth="February"; break;
		case 3: $BirthMonth="March"; break;
		case 4: $BirthMonth="April"; break;
		case 5: $BirthMonth="May"; break;
		case 6: $BirthMonth="June"; break;
		case 7: $BirthMonth="July"; break;
		case 8: $BirthMonth="August"; break;
		case 9: $BirthMonth="September"; break;
		case 10: $BirthMonth="October"; break;
		case 11: $BirthMonth="November"; break;
		case 12: $BirthMonth="December"; break;
		default: $BirthMonth="Date is wrong";		
	}
	$ClientBirthDate=$BirthMonth . " " . $BirthDay . ", " . $BirthYear;
	//EMail
	$ClientEMail=$ProfileArray["client_email"];
	//School
	$ClientSchool=$ProfileArray["client_school"];
	//Course
	$ClientCourse=$ProfileArray["client_coursename"];
	//CourseCode
	$ClientCourseCode=$ProfileArray["client_coursecode"];
	//YearGraduated
	$ClientYearGraduated=$ProfileArray["client_yeargraduated"];
	//Batch
	$ClientBatch=$ProfileArray["client_batch"];
	//Company
	$ClientCompany=$ProfileArray["client_company"];
	//Position
	$ClientPosition=$ProfileArray["client_position"];
	//TotalDonation
	$Donation= $ProfileArray["client_totaldonation"];
	$ClientTotalDonation="P " . $Donation;
	//Alumni
	$ClientAlumni=$ProfileArray["client_alumni"];
	//Deceased
	$ClientDeceased=$ProfileArray["client_deceased"];
	$ClientDateofDeath=$ProfileArray["client_dateofdeath"];
	$DeceaseMonth=substr($ClientDateofDeath,5,2);
	$DeceaseDay=substr($ClientDateofDeath,8,2);
	$DeceaseYear=substr($ClientDateofDeath,0,4);
	switch ($DeceaseMonth)
	{
		case 1: $DeceaseMonth="January"; break;
		case 2: $DeceaseMonth="February"; break;
		case 3: $DeceaseMonth="March"; break;
		case 4: $DeceaseMonth="April"; break;
		case 5: $DeceaseMonth="May"; break;
		case 6: $DeceaseMonth="June"; break;
		case 7: $DeceaseMonth="July"; break;
		case 8: $DeceaseMonth="August"; break;
		case 9: $DeceaseMonth="September"; break;
		case 10: $DeceaseMonth="October"; break;
		case 11: $DeceaseMonth="November"; break;
		case 12: $DeceaseMonth="December"; break;
		default: $DeceaseMonth="Date is wrong";		
	}
	$ClientDeceasedDate=$DeceaseMonth . " " . $DeceaseDay . ", " . $DeceaseYear;
}

$AddressesArray=$client->getAddresses($id);

$ClientAddresses="";
$EditAddressButton="edit";
$companyctr=0;

if($AddressesArray)
	{

	$adrsctr=0;
	
while($adrsctr < sizeof($AddressesArray))
{
	$Checker = $AddressesArray[$adrsctr];
	
	if($Checker["address_type"]=="Work")
	{
		$CompanyAddressArray=$Checker;
		$companyctr++;
	}
	else
	{
		$HousesStreet=$Checker["address_street"];
		$HousesMunicipality=$Checker["address_municipality"];
		$HousesCity=$Checker["address_city"];
		$HousesProvince=$Checker["address_province"];
		$HousesZipCode=$Checker["address_zipcode"];
		$HousesCountry=$Checker["address_country"];
		if($Checker["address_type"]=="Primary")
		$ClientAddresses="(P) " . $HousesStreet . " " . $HousesMunicipality . " " . $HousesCity . " " . $HousesProvince . " " . $HousesZipCode . " " . $HousesCountry . "<BR>" . $ClientAddresses;	
		else
		if($Checker["address_type"]=="Secondary")
		$ClientAddresses=$ClientAddresses . "(S) " . $HousesStreet . " " . $HousesMunicipality . " " . $HousesCity . " " . $HousesProvince . " " . $HousesZipCode . " " . $HousesCountry . "<BR>";
	
	$EditAddressButton="edit";
	}	
	
	$adrsctr++;
}

	if($companyctr==sizeof($AddressesArray))
	{
		$ClientAddresses="No Address yet";
		$EditAddressButton="add";
	}

	//Company Address
	if($companyctr>0)
	{
		$CompanyStreet=$CompanyAddressArray["address_street"];
		$CompanyMunicipality=$CompanyAddressArray["address_municipality"];
		$CompanyCity=$CompanyAddressArray["address_city"];
		$CompanyProvince=$CompanyAddressArray["address_province"];
		$CompanyZipCode=$CompanyAddressArray["address_zipcode"];
		$CompanyCountry=$CompanyAddressArray["address_country"];
		$ClientCompanyAddress=$CompanyStreet . " " . $CompanyMunicipality . " " . $CompanyCity . " " . $CompanyProvince . " " . $CompanyZipCode . " " . $CompanyCountry;
	}
	else
	$ClientCompanyAddress="No Address yet";
	
}
else
{
	$ClientAddresses="No Address yet";
	$ClientCompanyAddress="No Address yet";
	$EditAddressButton="add";
}

$Phones="";
$PhonesH="";
$PhonesC="";
$PhonesM="";
$EditPhoneButton="add";

	$PhonesArray=$client->getPhoneNumbers($id);
if($PhonesArray)
{
		$phnprio=0;
		$phnctr=0;
		$EditPhoneButton="edit";
	
while($phnctr < sizeof($PhonesArray))
{
	$Checker = $PhonesArray[$phnctr];
	
	if($phnprio==0 || $phnprio<=$Checker["phone_priority"])
	{
	$phnprio=$Checker["phone_priority"];
	}
	
	if($Checker["phone_type"]=="Home")
	{
		$HomePhoneArray=$Checker;

		$HomeCountryCode=$HomePhoneArray["phone_countrycode"];
		$HomeAreaCode=$HomePhoneArray["phone_areacode"];
		$HomeNumber=$HomePhoneArray["phone_number"];
		$ClientHomeNumber=$HomeCountryCode . "" . $HomeAreaCode . "" . $HomeNumber;
		
		if($Checker["phone_priority"]>=$phnprio)
		{
		$PhonesH=$PhonesH . "<font size=1>home:</font> " . $ClientHomeNumber . "&nbsp;";
		}
		else
		if($Checker["phone_priority"]<$phnprio)
		{
		$PhonesH="<font size=1>home:</font> " . $ClientHomeNumber . "&nbsp;" . $PhonesH;	
		}
		
	}
$phnctr++;
}

$Phones=$PhonesH;

$phnprio=0;
$phnctr=0;

while($phnctr < sizeof($PhonesArray))
{
	$Checker = $PhonesArray[$phnctr];
	
	if($phnprio==0 || $phnprio<=$Checker["phone_priority"])
	{
	$phnprio=$Checker["phone_priority"];
	}
	
	if($Checker["phone_type"]=="Office")
	{
		$CompanyPhoneArray=$Checker;

	$CompanyCountryCode=$CompanyPhoneArray["phone_countrycode"];
	$CompanyAreaCode=$CompanyPhoneArray["phone_areacode"];
	$CompanyNumber=$CompanyPhoneArray["phone_number"];
	$ClientCompanyNumber=$CompanyCountryCode . "" . $CompanyAreaCode . "" . $CompanyNumber;
		
		if($Checker["phone_priority"]>=$phnprio)
		{
		$PhonesC=$PhonesC . "<font size=1>company:</font> " . $ClientCompanyNumber . "&nbsp;";
		}
		else
		if($Checker["phone_priority"]<$phnprio)
		{
		$PhonesC="<font size=1>company:</font> " . $ClientCompanyNumber . "&nbsp;" . $PhonesC;	
		}
		
	}
$phnctr++;
}
$Phones=$Phones . "<BR>" . $PhonesC;

}

	$MobilesArray=$client->getMobileNumbers($id);
if($MobilesArray)
{

		$EditPhoneButton="edit";
		$phnprio=0;
		$phnctr=0;
	
while($phnctr < sizeof($MobilesArray))
{
	$Checker = $MobilesArray[$phnctr];
	
	if($phnprio==0 || $phnprio>=$Checker["mobile_priority"])
	{
	$phnprio=$Checker["mobile_priority"];
	}
	
		$MobileArray=$Checker;

		$MobileCountryCode=$MobileArray["mobile_countrycode"];
		$Provider=$MobileArray["mobile_provider"];
		$MobileNum=$MobileArray["mobile_number"];
		$ClientMobileNumber=$MobileCountryCode . "" . $Provider . "" . $MobileNum;
		
		if($Checker["mobile_priority"]>=$phnprio)
		{
		$PhonesM=$PhonesM . "<font size=1>mobile:</font> " . $ClientMobileNumber . "&nbsp;";
		}
		else
		if($Checker["phone_priority"]<$phnprio)
		{
		$PhonesM="<font size=1>mobile:</font> " . $ClientMobileNumber . "&nbsp;" . $PhonesM;	
		}
		

$phnctr++;
}	

$Phones=$PhonesM . "<BR>" . $Phones;

}

if($Phones=="")
{
$Phones="No contact number/s yet";
}


	##########################	
	# set template variables #
	##########################	


		$template->setVariable('PAGE_TITLE', $page_title);	
		$template->setVariable('STYLE', $css);
		$template->setVariable('absRoot', $absRoot);
		
		//Client Profile
		$template->setVariable('CLIENTID', $id);
		$template->setVariable('CLIENTTYPE', $ClientType);
		$template->setVariable('CLIENTNAME', $ClientName);
		$template->setVariable('CLIENTGENDER', $ClientGender);
		$template->setVariable('CLIENTBIRTHDATE', $ClientBirthDate);
		$template->setVariable('CLIENTADDRESS', $ClientAddresses);
		$template->setVariable('EDITADDRESS', $EditAddressButton);
		$template->setVariable('EDITPHONE', $EditPhoneButton);
		$template->setVariable('CLIENTPHONES', $Phones);
		$template->setVariable('CLIENTEMAIL', $ClientEMail);
		$template->setVariable('CLIENTSCHOOL', $ClientSchool);
		$template->setVariable('CLIENTCOURSE', $ClientCourse);
		$template->setVariable('CLIENTCOURSECODE', $ClientCourseCode);
		$template->setVariable('CLIENTYEARGRADUATED', $ClientYearGraduated);
		$template->setVariable('CLIENTBATCH', $ClientBatch);
		$template->setVariable('CLIENTCOMPANY', $ClientCompany);
		$template->setVariable('CLIENTPOSITION', $ClientPosition);
		$template->setVariable('CLIENTCOMPANYADDRESS', $ClientCompanyAddress);
		$template->setVariable('CLIENTTOTALDONATION', $ClientTotalDonation);
	if($ClientAlumni==1)
		$template->setVariable('CLIENTALUMNI', "checked");
	if($ClientDeceased==1)	
	{
		$template->setVariable('CLIENTDECEASED', "checked");
		$template->setVariable('CLIENTDECEASEDDATE', $ClientDeceasedDate);
	}

		//$template->setVariable('DASHBOARD_NOTIFICATION1_DATA', 'Made Calls');
		//$template->setVariable('XAJAX_INIT', $xajax_directives);

	#############################	
	# render the index template #
	#############################
		//$xajax->processRequests();	
		$template->show();		
?>	
